using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class PentaltyFeesController : Controller
    {
        public PentaltyFeesController(IRepository<PentaltyFee> pentaltyFeeRepository) {
            Check.Require(pentaltyFeeRepository != null, "pentaltyFeeRepository may not be null");

            this.pentaltyFeeRepository = pentaltyFeeRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<PentaltyFee> pentaltyFees = pentaltyFeeRepository.GetAll();
            return View(pentaltyFees);
        }

        [Transaction]
        public ActionResult Show(int id) {
            PentaltyFee pentaltyFee = pentaltyFeeRepository.Get(id);
            return View(pentaltyFee);
        }

        public ActionResult Create() {
            PentaltyFeeFormViewModel viewModel = PentaltyFeeFormViewModel.CreatePentaltyFeeFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(PentaltyFee pentaltyFee) {
            if (ViewData.ModelState.IsValid && pentaltyFee.IsValid()) {
                pentaltyFeeRepository.SaveOrUpdate(pentaltyFee);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The pentaltyFee was successfully created.";
                return RedirectToAction("Index");
            }

            PentaltyFeeFormViewModel viewModel = PentaltyFeeFormViewModel.CreatePentaltyFeeFormViewModel();
            viewModel.PentaltyFee = pentaltyFee;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            PentaltyFeeFormViewModel viewModel = PentaltyFeeFormViewModel.CreatePentaltyFeeFormViewModel();
            viewModel.PentaltyFee = pentaltyFeeRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(PentaltyFee pentaltyFee) {
            PentaltyFee pentaltyFeeToUpdate = pentaltyFeeRepository.Get(pentaltyFee.Id);
            TransferFormValuesTo(pentaltyFeeToUpdate, pentaltyFee);

            if (ViewData.ModelState.IsValid && pentaltyFee.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The pentaltyFee was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                pentaltyFeeRepository.DbContext.RollbackTransaction();

				PentaltyFeeFormViewModel viewModel = PentaltyFeeFormViewModel.CreatePentaltyFeeFormViewModel();
				viewModel.PentaltyFee = pentaltyFee;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(PentaltyFee pentaltyFeeToUpdate, PentaltyFee pentaltyFeeFromForm) {
			pentaltyFeeToUpdate.FeeType = pentaltyFeeFromForm.FeeType;
			pentaltyFeeToUpdate.Fee = pentaltyFeeFromForm.Fee;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The pentaltyFee was successfully deleted.";
            PentaltyFee pentaltyFeeToDelete = pentaltyFeeRepository.Get(id);

            if (pentaltyFeeToDelete != null) {
                pentaltyFeeRepository.Delete(pentaltyFeeToDelete);

                try {
                    pentaltyFeeRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the pentaltyFee from being deleted. " +
						"Another item likely depends on this pentaltyFee.";
                    pentaltyFeeRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The pentaltyFee could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the PentaltyFee form for creates and edits
		/// </summary>
        public class PentaltyFeeFormViewModel
        {
            private PentaltyFeeFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static PentaltyFeeFormViewModel CreatePentaltyFeeFormViewModel() {
                PentaltyFeeFormViewModel viewModel = new PentaltyFeeFormViewModel();
                
                return viewModel;
            }

            public PentaltyFee PentaltyFee { get; internal set; }
        }

        private readonly IRepository<PentaltyFee> pentaltyFeeRepository;
    }
}
